﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using BLL.Validation;
using DAL;
using Entities;
using System.Linq;
using WindowsApp.QLLopHoc;
using DAL.IRepository;

namespace WindowsApp.QLLopHoc
{
    public partial class frmQLLopHoc : DevComponents.DotNetBar.Office2007Form
    {

        private IGeneralRepository<LopHoc> _repository;
        private LopHocManager _manager;
        public TaiKhoan _tk;
        LopHoc _lophoc;
        private bool _selectAll = false;
        public frmQLLopHoc()
        {
            InitializeComponent();
            dgvDSLopHoc.AutoGenerateColumns = false;
        }

        private void frmQLLopHoc_Load(object sender, EventArgs e)
        {
            _repository = new GeneralRepository<LopHoc>();
            _manager = new LopHocManager(_repository);
            dgvDSLopHoc.DataSource = _repository.GetAll().ToList();
            XuLyPhanQuyen();

        }

        void XuLyPhanQuyen()
        {
            if (_tk.PhanQuyen.ToUpper() == "ADMIN")
            {
                btnThem.Enabled = true;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
                mnuThemLH.Enabled = true;
                mnuSuaLH.Enabled = true;
                mnuXoaLH.Enabled = true;
            }
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dgvDSLopHoc.DataSource = _repository.GetAll().ToList();
            txtTenLH.ResetText();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new frmAddLopHoc(_manager,this);
            frm.Text = "Thêm lớp học";
            frm.ShowDialog();
            dgvDSLopHoc.DataSource = _repository.GetAll().ToList();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa các lớp học đã chọn?", "Xóa lớp học", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvDSLopHoc.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    _manager.Delete(row.Cells[1].Value.ToString());
            }
            dgvDSLopHoc.DataSource = _repository.GetAll().ToList();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.MaLop.IndexOf(txtTenLH.Text) >= 0);
            if (aResult.Success)
                dgvDSLopHoc.DataSource = aResult.Result;
            else
            {
                MessageBox.Show(aResult.GetErrorMessages());
                btnLamMoi.PerformClick();
                txtTenLH.ResetText();
                txtTenLH.Focus();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvDSLopHoc.SelectedRows.Count > 0)
            {
                _lophoc = new LopHoc();
                DataGridViewRow row = dgvDSLopHoc.SelectedRows[0];
                _lophoc.MaLop = row.Cells[1].Value.ToString();
                _lophoc.SiSo =Convert.ToInt32( row.Cells[2].Value.ToString());

                var frm = new frmAddLopHoc(_manager,this, _lophoc);
                frm.Text = "Cập nhật lớp học";
                frm.ShowDialog();
                dgvDSLopHoc.DataSource = _repository.GetAll().ToList();
            }
            else
                MessageBox.Show("Chưa chọn lớp học để sửa", "Thông báo");

        }

        private void dgvDSLopHoc_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSLopHoc.Rows.Count > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < dgvDSLopHoc.Rows.Count; idx++)
                {
                    dgvDSLopHoc.Rows[idx].Selected = _selectAll;
                    dgvDSLopHoc.Rows[idx].Cells[0].Value = _selectAll;
                }
            }
        }

        private void dgvDSLopHoc_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvDSLopHoc.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool selected = (bool)dgvDSLopHoc.Rows[e.RowIndex].Cells[0].Value;
                dgvDSLopHoc.Rows[e.RowIndex].Selected = selected;
            }
        }

        private void txtTenLH_TextChanged(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.MaLop.IndexOf(txtTenLH.Text) >= 0);
            if (aResult.Success)
                dgvDSLopHoc.DataSource = aResult.Result;
            else
                dgvDSLopHoc.DataSource = null;
        }

    }
}
